#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName  :getDatas.py
# @Time      :2021/6/21 21:49
# @Author    : 刘广鸿
from url.urls import URLS
from config.config import shzx_app
from fastapi import Depends, status, Response
from sqlalchemy.orm import Session
from dbs import crud, mSession
from dbs import model
from dbs import schemas
from dbs.database import engine
import uuid
import hashlib
# import datetime
from time import time

model.Base.metadata.create_all(bind=engine)

"""
    Test For Get Mehod：http://127.0.0.1:8000/Devices/{username}/{token}
    Get datas about the Devices:
    :param
        @username : who want to get the Devices`s datas
        @token:     token for check The user's legal identity
    :return
        403: for Denial of service
        others: for the datas about the devices in the User-managed devices list
"""


@shzx_app.get(URLS[4])
def get_Devices(username: str, token: str, db: Session = Depends(mSession.get_db)):
    datalist = crud.query_devices(db, username, token)
    if not datalist:
        return Response(status_code=status.HTTP_403_FORBIDDEN)
    else:
        return datalist


"""
    Test for Get Method: http://127.0.0.1:8000/datas/{username}/{token}/{uuid}/{offset}/{limit}
    :param
        @username  ：who want to get the Device`s datas
        @token    ：token for check The user's legal identity
        @offset : where start in  the collection of data
        @limit: The number of data bars
    :return
        404 for No resources were found
        others for the datas_set of the device
"""

@shzx_app.get(URLS[5])
@shzx_app.get(URLS[6])
def get_Datas(username: str, token: str, uuid: str, offset: int = None, limit: int = None,
              db: Session = Depends(mSession.get_db)):
    datas_set = crud.query_datas(db, username, token, uuid, offset, limit)
    if not datas_set:
        return Response(status_code=status.HTTP_404_NOT_FOUND)
    else:
        return datas_set
